//
//  XHRIllDetailViewController.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/5.
//  Copyright © 2016年 xuhongru. All rights reserved.
//

#import "XHRIllDetailViewController.h"

#import "XHRNetworkingToolManager.h"

#import "XHRTextFieldRightView.h"
#import "XHRIllDetailTextField.h"
#import "WWSameSickVC.h"
#import "XHRSickBranchesController.h"
#import "XHRPayAttentionViewController.h"
#import "WWSameSickModel.h"
#import <SVProgressHUD.h>

// 本地文件的地址
#define Path [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"user.text"]


#import "XHRTreatedMethodViewController.h"

@interface XHRIllDetailViewController ()

/**标题label*/
@property(nonatomic,weak)UILabel *titlLabel;

/**疾病细分框*/
@property(nonatomic,weak)UITextField *selectIllField;

/**是否确诊文本框*/
@property(nonatomic,weak)UITextField *isSureField;

/**并发症*/
@property(nonatomic,weak)UITextField *selectComplicationField;

/**是否接受过治疗*/
@property(nonatomic,weak)UITextField *isTreatedField;

/**底部医生数量的label*/
@property(nonatomic,weak)UILabel *doctorNumberLabel;
/**医生头像*/
@property(nonatomic,weak)UIImageView *doctorImageView;

/**设置底部就诊按钮*/
@property(nonatomic,weak)UIButton *bottomButton;

/**受治疗的详细信息*/
@property(nonatomic,weak)UITextField *treatedMessageField;

// 含订阅信息的并发症数组
@property(nonatomic, strong)NSArray<WWSameSickModel *> *synSicks;

@end

@implementation XHRIllDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initiateSettings];
    [self setupUI];
}
- (void)initiateSettings
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"疾病详情选择";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"预约须知" style:UIBarButtonItemStyleDone target:self action:@selector(didClickRightButton:)];
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}
#pragma mark -
#pragma mark - 设置子控件
- (void)setupUI
{
    //设置标题label
    [self setupTitleLabel];
    //设置疾病细分的textField
    [self setupSelectIllField];
    //设置是否确诊textField
    [self setupIsSureField];
    //设置并发症textField
    [self setupSelectComplication];
    //设置是否接受过治疗textField
    [self setupIsTreated];
    //设置受治疗的详细信息
    [self setupTreatedMessageField];
    //设置申请就诊按钮
    [self setupBottomButton];
    //设置医生图片
    [self setupDoctorImage];
    //设置医生数量label
    [self setupDoctorNumberLabel];
}
#pragma mark -
#pragma mark - 设置标题label
- (void)setupTitleLabel
{
    //初始化label
    UILabel *label = [[UILabel alloc]init];
    label.text = [NSString stringWithFormat:@"疾病类型:%@",self.illName];
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor darkTextColor];
    
    [self.view addSubview:label];
    
    self.titlLabel = label;
    //设置约束
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.leading.offset(15);
    }];
}
#pragma mark -
#pragma mark - 设置疾病细分的textField
- (void)setupSelectIllField
{
    UITextField *textField = [self setupTextFieldWithType:XHRTextFieldRightViewTypeSigleButton placeholder:@"请选择疾病细分" withBlock:^{

        // MARK:   网络检测
        AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
        
        [manager startMonitoring];
        
        [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            /**
             AFNetworkReachabilityStatusUnknown          = -1,
             AFNetworkReachabilityStatusNotReachable     = 0,
             AFNetworkReachabilityStatusReachableViaWWAN = 1,
             AFNetworkReachabilityStatusReachableViaWiFi = 2,
             **/
            
            switch (status) {
                case -1:
                    
                    break;
                case AFNetworkReachabilityStatusNotReachable:
                    // 无法连接
                    // 添加遮罩 警示信息
                    [SVProgressHUD setMinimumDismissTimeInterval:1];
                    
                    [SVProgressHUD showErrorWithStatus:@"网络连接失败"];
                    
                    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                    
                    break;
                case 1:
                    // 通过WWAN连接
                    break;
                case 2:
                    // 通过WIFI连接
                    
                    break;
                    
                default:
                    break;
            }
            
        }];
        
        XHRSickBranchesController * sickBranchesVC = [XHRSickBranchesController new];
        
        sickBranchesVC.backBlock = ^(NSString *sick){
            self.selectIllField.text = sick;
        };
        
        [self.navigationController pushViewController:sickBranchesVC animated:YES];
        
    }];
    self.selectIllField = textField;
    
    [textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlLabel.bottom).offset(30);
        make.leading.equalTo(self.titlLabel);
        make.trailing.offset(-15);
        make.height.equalTo(30);
    }];
}
#pragma mark -
#pragma mark - 设置是否确诊textField
- (void)setupIsSureField
{
    UITextField *textField = [self setupTextFieldWithType:XHRTextFieldRightViewTypeDoubleButtonIsSure placeholder:@"是否确诊" withBlock:^{
        
    }];
    
    self.isSureField = textField;
    
    [textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.selectIllField.bottom).offset(20);
        make.leading.equalTo(self.titlLabel);
        make.trailing.offset(-15);
        make.height.equalTo(30);
    }];
}

#pragma mark -
#pragma mark - 设置并发症textField

//  MARK:  显示内容到并发病的textField上
- (void)showSicks2TFWith:(NSArray<WWSameSickModel *> *)arr{
    
    NSMutableArray *mArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(WWSameSickModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.taped) {
            [mArr addObject:obj.title];
        }
    }];

    self.selectComplicationField.text =  [mArr.copy componentsJoinedByString:@"."];
}


- (void)setupSelectComplication
{
    UITextField *textField = [self setupTextFieldWithType:XHRTextFieldRightViewTypeSigleButton placeholder:@"请选择并发症(可多选)" withBlock:^{
//        NSLog(@"请选择并发症(可多选)");
        WWSameSickVC *sameSickVC = [WWSameSickVC new];
        
        // 将文件内容解档 如果有将数组赋给sameSickVC ; 如果文件读取不到 将nil给sameSickVC
        self.synSicks = [NSKeyedUnarchiver unarchiveObjectWithFile:Path];
         
        sameSickVC.synSicks = self.synSicks;
        
        [self.navigationController pushViewController:sameSickVC animated:YES];
        sameSickVC.sicksBlock = ^(NSArray *arr){
           
            // 将数组中疾病显示出来
            [self showSicks2TFWith:arr];
            
            // 将数组归档到Document文件夹
            [NSKeyedArchiver archiveRootObject:arr toFile:Path];
        };
    }];
    
    self.selectComplicationField = textField;
    
    [textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.isSureField.bottom).offset(20);
        make.leading.equalTo(self.titlLabel);
        make.trailing.offset(-15);
        make.height.equalTo(30);
    }];
}
#pragma mark -
#pragma mark - 设置 TreatedMessageField
- (void)setupTreatedMessageField
{
    __weak typeof(self)weakSelf = self;
    UITextField *textField = [self setupTextFieldWithType:XHRTextFieldRightViewTypeSigleButton placeholder:@"请选择治疗方式" withBlock:^{
        XHRTreatedMethodViewController *treatedMethodVC = [[XHRTreatedMethodViewController alloc]init];
        
        treatedMethodVC.treatedMethodBlock = ^(NSString *treatedMethod){
            weakSelf.treatedMessageField.text = treatedMethod;
        };
        [weakSelf.navigationController pushViewController:treatedMethodVC animated:YES];
        
    }];
    self.treatedMessageField = textField;
    
    [textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.isTreatedField.bottom).offset(20);
        make.leading.equalTo(self.titlLabel);
        make.trailing.offset(-15);
        make.height.equalTo(30);
    }];
    
    textField.hidden = YES;
}
#pragma mark -
#pragma mark - 设置是否接受过治疗textField
- (void)setupIsTreated
{
    
    UITextField *textField = [self setupTextFieldWithType:XHRTextFieldRightViewTypeDoubleButtonIsTreated placeholder:@"是否接受过治疗" withBlock:^{
        
    }];
    
    
    self.isTreatedField = textField;
    
    [textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.selectComplicationField.bottom).offset(20);
        make.leading.equalTo(self.titlLabel);
        make.trailing.offset(-15);
        make.height.equalTo(30);
    }];
}
#pragma mark -
#pragma mark - 设置底部就诊申请按钮
- (void)setupBottomButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = XHRColorWithRGB(32, 198, 198) ;
    [button setTitle:@"就诊申请" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didClickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    self.bottomButton = button;
    [self.view addSubview:button];
    
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-30);
        make.centerX.equalTo(self.view);
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.height.equalTo(40);
    }];
}
#pragma mark -
#pragma mark - 设置底部匹配的医生的数目label
- (void)setupDoctorNumberLabel
{
    //初始化底部label
    UILabel *doctorNumberLabel = [[UILabel alloc]init];
    doctorNumberLabel.textAlignment = NSTextAlignmentCenter;
    doctorNumberLabel.text = @"重庆诊所为您匹配到0名医生";
    doctorNumberLabel.textColor = [UIColor lightGrayColor];
    doctorNumberLabel.font = [UIFont systemFontOfSize:13];
    self.doctorNumberLabel = doctorNumberLabel;
    [self.view addSubview:doctorNumberLabel];
    
    //设置约束
    [doctorNumberLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.offset(0);
        make.bottom.equalTo(self.doctorImageView.top).offset(-10);
        make.height.equalTo(30);
    }];
}
#pragma mark -
#pragma mark - setupDoctorImage
- (void)setupDoctorImage
{
    UIImageView *doctoarImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_person.png"]];
    self.doctorImageView = doctoarImage;
    [self.view addSubview:doctoarImage];
    
    doctoarImage.contentMode = UIViewContentModeScaleAspectFit;
    [doctoarImage makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.offset(0);
        make.bottom.equalTo(self.bottomButton.top).offset(-10);
    }];
}
#pragma mark -
#pragma mark - 设置文本框的方法
- (UITextField *)setupTextFieldWithType:(XHRTextFieldRightViewType)type placeholder:(NSString *)placeholder withBlock:(void (^)())clickBlock;
{
    __weak typeof(self) weakSelf = self;
    XHRIllDetailTextField *textField = [[XHRIllDetailTextField alloc]init];
    textField.type = type;
    textField.loadMoreBlock = clickBlock;
    
    textField.illDetailTextFieldBlock = ^(BOOL isTreated){
        if (isTreated) weakSelf.treatedMessageField.hidden = NO;
        else weakSelf.treatedMessageField.hidden = YES;
    };
    //设置占位文字属性
    NSMutableDictionary *propertyDict = [NSMutableDictionary dictionary];
    propertyDict[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [textField setupPlaceholderAttributes:propertyDict.copy placeholder:placeholder];
    [self.view addSubview:textField];
    return textField;
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickBottomButton:(UIButton *)sender
{
//    NSLog(@"%s",__func__);
    
    //MARK: - 跳转页面
    XHRPayAttentionViewController * attention = [[XHRPayAttentionViewController alloc]init];
    [self.navigationController pushViewController:attention animated:YES];
    
    //MARK: - 设置title
    attention.navigationItem.title = @"关注医生";
    

}
- (void)didClickRightButton:(UIBarButtonItem *)sender
{
    NSLog(@"%s",__func__);
}



@end
